package com.huishu.order.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.order.pojo.model.ProvinceFreightDO;
import com.huishu.order.service.ProvinceFreightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author generator@ljj
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/province-freight")
public class ProvinceFreightController {

    @Autowired
    private ProvinceFreightService provinceFreightService;


    @PostMapping("/getFreight/{province}")
    public Result getFreight(@PathVariable String province) {
        return provinceFreightService.getFreight(province);
    }


    @PostMapping("/pageList")
    public Result pageList(@RequestBody BasePage basePage) {
        return provinceFreightService.pageList(basePage);
    }

    @PostMapping("/update-delivery")
    public Result updateDelivery(@RequestBody ProvinceFreightDO provinceFreightDO) {
        LambdaUpdateWrapper<ProvinceFreightDO> uw = Wrappers.lambdaUpdate(ProvinceFreightDO.class)
                .set(ProvinceFreightDO::getIsDelivery, provinceFreightDO.getIsDelivery())
                .eq(ProvinceFreightDO::getId, provinceFreightDO.getId());
        return provinceFreightService.update(uw) ? Result.success() : Result.fail();
    }

    @PostMapping("/create")
    public Result create(@RequestBody ProvinceFreightDO provinceFreightDO) {
        return provinceFreightService.save(provinceFreightDO) ? Result.success() : Result.fail();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ProvinceFreightDO provinceFreightDO) {
        return provinceFreightService.updateById(provinceFreightDO) ? Result.success() : Result.fail();
    }

    @DeleteMapping("/delete/{id}")
    public Result update(@PathVariable Long id) {
        return provinceFreightService.removeById(id) ? Result.success() : Result.fail();
    }
}
